
export default {
    namespaced: true,
    state: () => ({
        address: JSON.parse(uni.getStorageSync('address') || '{}'),
        token:  JSON.parse(uni.getStorageSync('token') || '{}'),
        userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
        redirectinfo: null
    }),
    mutations: {
        // 更改收货地址
        updateAddress(state, newAddr) {
            state.address = newAddr

            this.commit('m_user/saveAddressToStorage')
        },
        // 收货地址本地缓存
        saveAddressToStorage(state) {
            uni.setStorageSync('address', JSON.stringify(state.address))
        },

        // 更新用户信息
        updateUserInfo(state, userinfo){
            state.userinfo = userinfo
            this.commit('m_user/saveUserInfoStorage')
        },
        // 缓存用户信息
        saveUserInfoStorage(state){
            uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
        },
        // 更新tokoen
        updateToken(state, token){
            state.token = token
            this.commit('m_user/saveTokenToStorage')
        },
        // 缓存token
        saveTokenToStorage(state){
            uni.setStorageSync('token', JSON.stringify(state.token))
        },
        // 更改返回界面信息
        updateRedirectInfo(state, info) {
            state.redirectinfo = info
            console.log(state.redirectinfo)
        }

    },
    getters: {
        addressStr(state) {
            if (!state.address.provinceName) return "";

            return (
                state.address.provinceName +
                state.address.cityName +
                state.address.countyName +
                state.address.detailInfo
            );
        },
    }
}